package demo10_4;

import java.util.Arrays;
import java.util.Comparator;

//class Student implements Comparable<Student>{
class Student{
    public String name;
    public int age;
    public Student(String name,int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

//    @Override
//    public int compareTo(Student o) {
//        //return this.age - o.age;
//        //谁调用compareTo方法 谁就是this
//        if (this.age > o.age) {
//            return 1;
//        }else if (this.age < o.age) {
//            return -1;
//        }else {
//            return 0;
//        }
//    }
}

class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test10_4 {
    public static void main(String[] args) {
        Student student1 = new Student("zhangsan",10);
        Student student2 = new Student("lisi",15);
        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(student1, student2));
    }
    public static void main3(String[] args) {
        Student student1 = new Student("张三",10);
        Student student2 = new Student("李四",15);
        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(student1, student2));
    }
    public static void bubbleSort(Comparable[] comparable) {
        for (int i = 0; i < comparable.length; i++) {
            for (int j = 0; j < comparable.length-1-i; j++) {
                if (comparable[j].compareTo(comparable[j+i]) > 0) {
                    Comparable temp = comparable[j];
                    comparable[j] = comparable[j+1];
                    comparable[j+1] = temp;
                }
            }
        }
    }
    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("张三",10);
        students[1] = new Student("李四",5);
        students[2] = new Student("王五",18);
//        bubbleSort(students);
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }
//    public static void main1(String[] args) {
//        Student student1 = new Student("张三",10);
//        Student student2 = new Student("李四",15);
////        System.out.println(student1 > student2);
//        System.out.println(student1.compareTo(student2));//student1这个对象和student2这个对象比较
//    }
}
